package com.intct.flink.study;

import com.intct.func.TestFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @author gufg
 * @since 2025-10-13 15:25
 */
public class SQLTest05 {
    public static void main(String[] args) {
        // 配置属性
        Configuration conf = new Configuration();
        conf.set(RestOptions.BIND_PORT, "8081");

        // 1 获取取环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        // 并行度
        // env.setParallelism(1);
        // 开启检点
        env.enableCheckpointing(5000L, CheckpointingMode.EXACTLY_ONCE);
        // 获取SQL执行环境
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);

        // 注册函数
        tenv.createTemporarySystemFunction("test_fun", TestFunction.class);

        tenv.executeSql(
                "CREATE TABLE ws (\n" +
                        "  id INT,\n" +
                        "  vc INT,\n" +
                        "  pt AS PROCTIME(), --处理时间\n" +
                        "  et AS cast(CURRENT_TIMESTAMP as timestamp(3)), --事件时间\n" +
                        "  WATERMARK FOR et AS et - INTERVAL '5' SECOND   --watermark\n" +
                        ") WITH (\n" +
                        "  'connector' = 'datagen',\n" +
                        "  'rows-per-second' = '10',\n" +
                        "  'fields.id.min' = '1',\n" +
                        "  'fields.id.max' = '3',\n" +
                        "  'fields.vc.min' = '1',\n" +
                        "  'fields.vc.max' = '100'\n" +
                        ")");

        tenv.executeSql("select id,vc, test_fun(id, vc) as test from ws").print();
    }
}
